﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.ServiceComponent.DAC.Service;
using T.FAS.Runtime.Base.Entity;

namespace T.FAS.Runtime.DataModel.Dao
{
    public class DatabaseConfigELBDao : DaoService<DatabaseConfig>, IFASDatabaseConfigDao
    {
        public const string MetaDataID = "Database";
        public const string MetaDataName = "数据库";

        public override string TableName => "FAS_DatabaseConfig";
        public override string IDColumnName => "ID";
        public string ForeignColumn => "DatabaseID";

        public DatabaseConfigELBDao() : base()
        {
            DBName = "TFAS";
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public List<DatabaseConfig> GetDatabaseConfigs(string databaseID)
        {
            var result = new List<DatabaseConfig>();
            if (string.IsNullOrEmpty(databaseID))
            {
                return result;
            }
            List<object> objects = base.GetObjectsByFilter($"{ForeignColumn}='{databaseID}'");
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as DatabaseConfig));
            }
            return result;
        }

        /// <summary>
        /// 添加DatabaseConfig
        /// </summary>
        /// <param name="databaseConfigs"></param>
        public void CreateDatabaseConfig(List<DatabaseConfig> databaseConfigs)
        {
            try
            {
                base.Add(databaseConfigs);
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DB.CreateCfgError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除DatabaseConfig
        /// </summary>
        /// <param name="DatabaseConfigs"></param>
        public void DeleteDatabaseConfig(string databaseID)
        {
            try
            {
                base.Delete(GetDatabaseConfigs(databaseID).Select(o => o.ID).ToList());
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DB.DeleteCfgError, e.Message, e);
            }
        }

        public List<DatabaseConfig> GetDatabaseConfigs(Dictionary<string, string> filter)
        {
            var result = new List<DatabaseConfig>();
            List<object> objects = base.GetObjectsByFilter($"{ForeignColumn}='{filter["ID"]}'");
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as DatabaseConfig));
            }
            return result;
        }

        public DatabaseConfig GetDatabaseConfigByID(string dbCfgId)
        {
            return base.GetObject(dbCfgId);
        }

        public void AddDatabaseConfig(DatabaseConfig dbCfg)
        {
            try
            {
                base.Add(new List<object> { dbCfg });
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DB.CreateCfgError, e.Message, e);
            }
        }

        public void AddDatabaseConfigList(ArrayList dbCfgs)
        {
            try
            {
                List<object> list = new List<object>();
                foreach (var cfg in dbCfgs)
                {
                    list.Add(cfg);
                }
                base.Add(list);
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DB.CreateCfgError, e.Message, e);
            }
        }

        public void UpdateDatabaseConfig(DatabaseConfig dbCfg)
        {
            try
            {
                using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions(){IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted}))
                {
                    base.Delete(new List<string> { dbCfg.ID });
                    AddDatabaseConfig(dbCfg);
                    scope.Complete();
                }
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DB.UpdateCfgError, e.Message, e);
            }
        }

        public void DeleteDatabaseConfig(Dictionary<string, string> filter)
        {
            try
            {
                base.Delete(GetDatabaseConfigs(filter["DatabaseID"]).Select(cfg => cfg.ID).ToList());
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DB.DeleteCfgError, e.Message, e);
            }
        }
    }
}
